<?php
	function categoryGetAllCategoryAds($allCategories,$categoryAds) {
		global $globalDebug;
		$allRecursiveAds = array();
		
		foreach ($allCategories as $category=>$master) {
			if ($category != 0 && count($categoryAds[$category]) > 0) {
				categoryAddRecursiveAds($categoryAds[$category],$category,$allCategories,$allRecursiveAds);
			}
		}
		
		if ($globalDebug) {
			print "categoryGetAllCategoryAds: \n";
			print_r($allRecursiveAds);
		}
		
		return $allRecursiveAds;
	}
	
	#follow masters and merge the ads with existing ads
	function categoryAddRecursiveAds($ads,$category,$allCategories,&$allRecursiveAds) {
		global $globalDebug;
		
		if (is_array($allRecursiveAds[$category])) {
			#merge arrays
			$arr1 = array_keys($ads);
			$arr2 = array_keys($allRecursiveAds[$category]);
			$merge = array_merge($arr1,$arr2);
			$unique = array_unique($merge);
			$allRecursiveAds[$category] = array();
			foreach ($unique as $ad_id) {
				$allRecursiveAds[$category][$ad_id] = $ad_id;
			}
		} else {
			$allRecursiveAds[$category] = $ads;
		}
		
		if ($globalDebug) {
			print "got:";
			print_r($allRecursiveAds[$category]);
		}
		
		if ($category != 0) {
			categoryAddRecursiveAds($ads,$allCategories[$category],$allCategories,$allRecursiveAds);
		}
	}
?>
